package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbBusinessAgentApi;
import com.cq.hd.member.api.TbBusinessGroupApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.BusinessGroupAgentPageVo;
import com.cq.hd.member.api.vo.BusinessGroupPageVo;
import com.cq.hd.member.api.vo.BusinessSelectAgentGroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/group")
@Api(tags = "商家分组相关接口")
public class BusinessGroupController {

    @Autowired
    private TbBusinessGroupApi businessGroupApi;

    @Autowired
    private TbBusinessAgentApi businessAgentApi;

    @ApiOperation(value = "分组下拉列表")
    @ApiResponse(response = BusinessGroupPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/selectList")
    public Result<List<BusinessGroupPageVo>> selectList(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        BusinessGroupPageDto businessGroupPageDto = new BusinessGroupPageDto();
        businessGroupPageDto.setBusinessId(businessIdAgentIdDto.getBusinessId());
        businessGroupPageDto.setAgentId(businessIdAgentIdDto.getAgentId());

        return businessGroupApi.selectList(businessGroupPageDto);
    }

    @ApiOperation(value = "我的分组列表")
    @ApiResponse(response = BusinessGroupPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageGroup")
    public Result<Page<BusinessGroupPageVo>> pageBusinessGroup(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                                               BusinessGroupPageDto businessGroupPageDto) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        businessGroupPageDto.setBusinessId(businessIdAgentIdDto.getBusinessId());
        businessGroupPageDto.setAgentId(businessIdAgentIdDto.getAgentId());
        return businessGroupApi.pageBusinessGroup(businessGroupPageDto);
    }

    @ApiOperation(value = "保存分组")
    @ApiImplicitParam(name = "businessGroupSaveDto", value = "实体", required = true, paramType = "body", dataType = "BusinessGroupSaveDto")
    @ApiResponse(response = Long.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/saveGroup")
    public Result<Long> saveBusinessGroup(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                             @RequestBody BusinessGroupSaveDto businessGroupSaveDto) {
        String groupName = businessGroupSaveDto.getGroupName();
        if (StringUtils.isBlank(groupName)) {
            Throw.isBusinessException("请输入分组名称");
        }

        // 去除空格，换行等
        groupName = groupName.replaceAll("\\s+", "");
        if (StringUtils.isBlank(groupName)) {
            Throw.isBusinessException("请输入分组名称");
        }
        businessGroupSaveDto.setGroupName(groupName);

        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        businessGroupSaveDto.setBusinessId(businessIdAgentIdDto.getBusinessId());
        businessGroupSaveDto.setAgentId(businessIdAgentIdDto.getAgentId());

        return businessGroupApi.saveBusinessGroup(businessGroupSaveDto);
    }

    @ApiOperation(value = "删除分组")
    @ApiImplicitParam(name = "id", value = "分组id", required = true, paramType = "body", dataType = "Long")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @DeleteMapping("/deleteGroup")
    public Result<Boolean> deleteBusinessGroup(@ApiIgnore @LoginUser AppLoginUser appLoginUser, Long id) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        return businessGroupApi.deleteBusinessGroup(id, businessIdAgentIdDto.getBusinessId(), businessIdAgentIdDto.getAgentId());
    }

    @ApiOperation(value = "分组成员列表")
    @ApiResponse(response = BusinessGroupAgentPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageGroupAgent")
    public Result<Page<BusinessGroupAgentPageVo>> pageBusinessGroupAgent(@ApiIgnore @LoginUser AppLoginUser appLoginUser, BusinessGroupAgentPageDto businessGroupAgentPageDto) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        businessGroupAgentPageDto.setBusinessId(businessIdAgentIdDto.getBusinessId());
        businessGroupAgentPageDto.setAgentId(businessIdAgentIdDto.getAgentId());
        return businessAgentApi.pageBusinessGroupAgent(businessGroupAgentPageDto);
    }

    @ApiOperation(value = "分组成员批量移动、批量删除")
    @ApiImplicitParam(name = "businessGroupBatchChangeGroupDto", value = "实体", required = true, paramType = "body", dataType = "BusinessGroupBatchChangeGroupDto")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/batchChangeGroup")
    public Result<Boolean> batchChangeGroup(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                            @RequestBody BusinessGroupBatchChangeGroupDto businessGroupBatchChangeGroupDto) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        businessGroupBatchChangeGroupDto.setBusinessId(businessIdAgentIdDto.getBusinessId());
        businessGroupBatchChangeGroupDto.setAgentId(businessIdAgentIdDto.getAgentId());
        return businessAgentApi.batchChangeGroup(businessGroupBatchChangeGroupDto);
    }

    @ApiOperation(value = "删除分组成员")
    @ApiImplicitParam(name = "id", value = "分组成员id", required = true, paramType = "query", dataType = "Long")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @DeleteMapping("/deleteGroupAgent")
    public Result<Boolean> deleteBusinessGroupAgent(@ApiIgnore @LoginUser AppLoginUser appLoginUser, Long id) {
        BusinessIdAgentIdDto businessIdAgentIdDto = getBusinessIdAnAgentId(appLoginUser);
        return businessAgentApi.deleteBusinessGroupAgent(id, businessIdAgentIdDto.getBusinessId(), businessIdAgentIdDto.getAgentId());
    }

    @ApiOperation(value = "选择团长列表（供创建活动时设置分销佣金调用）")
    @ApiResponse(response = BusinessSelectAgentGroupVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listBusinessSelectAgent")
    public Result<List<BusinessSelectAgentGroupVo>> listBusinessSelectAgent(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        Long businessId = 0L;
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            businessId = businessInfo.getId();
        }

        Long userId = appLoginUser.getUserId();
        return businessAgentApi.listBusinessSelectAgent(userId, businessId);
    }

    private BusinessIdAgentIdDto getBusinessIdAnAgentId(AppLoginUser appLoginUser) {
        BusinessIdAgentIdDto businessIdAgentIdDto = new BusinessIdAgentIdDto();
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            businessIdAgentIdDto.setBusinessId(businessInfo.getId());
        } else {
            businessIdAgentIdDto.setAgentId(appLoginUser.getUserId());
        }
        return businessIdAgentIdDto;
    }
}
